﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Collections.ObjectModel;
using System.Windows;
using Microsoft.Practices.Prism.Commands;
using WpfPostgresql.Model;

namespace WpfPostgresql.ViewModel
{
    class MagazinsViewModel: SkladViewModel
    {
        private ObservableCollection<Databases.Posilka> _posilkas;
        public ObservableCollection<Databases.Posilka> Posilkas
        {
            get { return _posilkas; }
            set { _posilkas = value; RaisePropertyChanged("Posilkas"); }
        }
        public ObservableCollection<Databases.Magaz> Magazs { get; set; }
        public ObservableCollection<Databases.Tovar> TovarInPosilka { get; set; }

        private ObservableCollection<Databases.Tovar> _tovarInMagazs;
        public ObservableCollection<Databases.Tovar> TovarInMagazs
        {
            get { return _tovarInMagazs; }
            set { _tovarInMagazs = value; RaisePropertyChanged("TovarInMagazs"); }
        }

        private Databases.Voditel _voditelPosilki;
        public Databases.Voditel VoditelPosilki
        {
            get { return _voditelPosilki; }
            set { _voditelPosilki = value; RaisePropertyChanged("VoditelPosilki"); }
        }

        private Databases.Magaz _magazPosilki;
        public Databases.Magaz MagazPosilki
        {
            get { return _magazPosilki; }
            set { _magazPosilki = value; RaisePropertyChanged("MagazPosilki"); }
        }

        private Databases.Magaz _selectedMagazs;
        public Databases.Magaz SelectedMagazs
        {
            get { return _selectedMagazs; }
            set {
                _selectedMagazs = value; 
                Refresh();
                RaisePropertyChanged("SelectedMagazs");
            }
        }

        private Databases.Posilka _selectedPosilka;
        public Databases.Posilka SelectdPosilka
        {
            get { return _selectedPosilka; }
            set 
            {
                _selectedPosilka = value;
                RaisePropertyChanged("SelectdPosilka");
                RefreshPosilkaInfo();
                Refresh(); 
            }
        }

        public MagazinsViewModel()
        {
            _subtractCommand = new DelegateCommand<Databases.Tovar>(SubtractTovar);
            _delCommand = new DelegateCommand<Databases.Tovar>(DelTovar);
            _accept = new DelegateCommand(Accept);
            Magazs = new ObservableCollection<Databases.Magaz>(Databases.GetMagazList());
            if (Magazs.Count > 0)
            {
                _selectedMagazs = Magazs.First();
                _tovarInMagazs = new ObservableCollection<Databases.Tovar>(Databases.GetTovarListByMagazId(_selectedMagazs.ID, this));
            }
            Posilkas = new ObservableCollection<Databases.Posilka>(Databases.GetPosilks());
            if (Posilkas.Count>0)
            {
                SelectdPosilka = Posilkas.First();
            }
            
        }

        private readonly DelegateCommand<Databases.Tovar> _subtractCommand;
        public DelegateCommand<Databases.Tovar> SubtractCommand
        {
            get
            {
                return _subtractCommand;
            }
        }
        private void SubtractTovar(Databases.Tovar tovar)
        {
            Databases.AddColTovarById(tovar.ID, -1);
            (tovar.ParentMagazVm).Refresh();
        }

        private readonly DelegateCommand<Databases.Tovar> _delCommand;
        public DelegateCommand<Databases.Tovar> DelCommand
        {
            get
            {
                return _delCommand;
            }
        }
        private void DelTovar(Databases.Tovar tovar)
        {
            try
            {
                Databases.DeleteTovarInMagazinById(tovar.ID);
                Databases.DeleteTovarById(tovar.ID);
                (tovar.ParentMagazVm).Refresh();
            }
            catch (Exception)
            {
                MessageBox.Show("Ошибка оО");
            }

        }


        private readonly DelegateCommand _accept;
        public DelegateCommand AcceptCommand
        {
            get
            {
                return _accept;
            }
        }
        private void Accept()
        {
            if (SelectdPosilka == null) return;
            var tempTovarList = Databases.GetTovarListByPosilkaId(SelectdPosilka.ID);
            foreach (var tovar in tempTovarList)
            {
                Databases.AddMagzinTovar(new Databases.MagzinTovar{MagazinID = SelectedMagazs.ID, TovarID = tovar.ID});
            }
            Databases.DeletePosilkaByPosilkaID(SelectdPosilka);
        }

        public void Refresh()
        {
            TovarInMagazs = new ObservableCollection<Databases.Tovar>(Databases.GetTovarListByMagazId(SelectedMagazs.ID, this));
            RaisePropertyChanged("TovarInMagazs");
            if (Posilkas.Count > 0)
            {
                TovarInPosilka =
                    new ObservableCollection<Databases.Tovar>(Databases.GetTovarListByPosilkaId(SelectdPosilka.ID));

                RefreshPosilkaInfo();
            }
        }

        public void RefreshPosilkaInfo()
        {
            MagazPosilki = Databases.GetMagazinlByPosilkaId(SelectdPosilka.ID);
            VoditelPosilki = Databases.GetVoditelByPosilkaId(SelectdPosilka.ID);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}
